package com.zthz.douxing.util

import com.zthz.douxing.MessageToUserLog
import com.zthz.douxing.PlanTripSaveType
import com.zthz.douxing.TripSegment
import grails.transaction.Transactional
import groovy.text.GStringTemplateEngine
import org.codehaus.groovy.grails.web.context.ServletContextHolder
import redis.clients.jedis.Jedis

@Transactional
class CommonUtilService {

    def gsonBuilder
    def redisService
    def config
    def grailsApplication

    public  Map getJSON(object){
        def gBuilser = null
        def result = null
        try{
            gBuilser = gsonBuilder.create()
            result =  gBuilser.fromJson(object,Map)
        }catch (Exception e){
            log.debug(e.printStackTrace())
        }
        return result
    }

    public Map getDataFromJson(String json){
        def gBuilser = null
        def result = null
        try{
            gBuilser = gsonBuilder.create()
            result =  gBuilser.fromJson(json,Map)
        }catch (Exception e){
            log.debug(e.printStackTrace())
        }
        return result
    }


    public String getDateFromRedis(String key){
        def result = null
        redisService.withRedis {Jedis redis ->
               result = redis.get(key)
        }
        return result
    }
    /**
     * @param config中定义的执行类List名字
     * */
    public List getConfigDataList(data){
        return getHandlerRefs(data)
    }

    private getHandlerRefs(dataList){
        def handlerRefs = null
        def handlers = getConfig(dataList).handlers
        if(handlers?.size()>0){
            handlerRefs = createRefList(handlers)
        }
        return handlerRefs
    }
    private createRefList = { names -> names.collect { name -> getBean(name) } }


    def getConfig(dataList){
        return  grailsApplication.config.data.getProperty(dataList)
    }


    public  updateTripSegmentStauts(TripSegment ts,PlanTripSaveType saveType){
        try{
            if(ts.save()){
                def plant = ts.plantrip
                if(saveType)
                    plant.planTripSaveType = saveType
                if(plant.save())
                    return true
                else
                    return false
            }else
                return false
        }catch (Exception e){
            throw e
        }

    }
    public void save2Redis(String key,String content){
        redisService.withRedis {Jedis redis ->
            redis.set(key,content)
        }
    }
    public void save2RedisWithExpireTime(String key,String content,time){
        redisService.withRedis {Jedis redis ->
            redis.set(key,content)
            redis.expire(key,time)
        }
    }

    public getFromRedis(String key){
        def result = null
        redisService.withRedis {Jedis redis ->
            result = redis.get(key)
        }
        return result
    }
    public deleteRedisData(String key){
        redisService.withRedis {Jedis redis ->
            redis.del(key)
        }
    }
    public getBean(String beanName){
        return grailsApplication.mainContext.getBean(beanName)
    }

    public getProperties(String key){

        if(key){
            String porPath = ServletContextHolder.getServletContext().getRealPath("/fileuploadpath.properties")
            Properties pro = new Properties()
            pro.load(new InputStreamReader(new FileInputStream(porPath),"UTF-8"))
            return pro.getProperty(key)
        }
        else
            return null
    }
    public getTemplateString(String templateKey,HashMap para){

        if(templateKey && para){
            String template = getProperties(templateKey)
            if(templateKey){
                String res = ""
                try{
                    def gse = new GStringTemplateEngine()
                    res = gse.createTemplate(template).make(para).toString()
                }catch (Exception e){
                    log.debug(e.getMessage(),e.printStackTrace())
                }
                return res
            }else
                return null
        }else
            return null


    }
}
